﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CCH.DAO;
using CCH.Entity;

public partial class _50_5002_1 : System.Web.UI.Page
{
    protected string you_code = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            sysfunctionDAO dao = new sysfunctionDAO();

            this.DropDownList1.DataBind();

            if (Request.QueryString["mode"] == "new")
            {
                this.lab_title.Text = dao.Get_titleName(5002) + " - 新增";
            }
            else
            {
                this.lab_title.Text = dao.Get_titleName(5002) + " - 修改";
                this.hidd_no.Value = Request.QueryString["ID"];
                int you_no = int.Parse(this.hidd_no.Value);

                youtube data = new youtubeDAO().Get_youtube(you_no);

                this.tbox_name.Text = data.you_name.ConvertToNewLine();
                this.tbox_desc.Text = data.you_describe.ConvertToNewLine();
                this.tbox_link.Text = data.you_link;
                this.DropDownList1.Items.FindByValue(data.you_clano.ToString()).Selected=true;

            }


        }
    }

    protected void btn_ok_Click(object sender, EventArgs e)
    {
        if (this.check_Input())
        {
            string str = "";
            youtube data;
            youtubeDAO dao = new youtubeDAO();

            if (this.hidd_no.Value != "")
            {
                data = dao.Get_youtube(int.Parse(this.hidd_no.Value));
                str = "修改完成!";
            }
            else
            {
                data = new youtube();
                str = "新增完成!";
                data.state = "1";
            }

            data.you_clano = int.Parse(this.DropDownList1.SelectedItem.Value);
            data.you_name = this.tbox_name.Text.ConvertToBR();
            data.you_describe = this.tbox_desc.Text.ConvertToBR();
            data.you_link = this.tbox_link.Text;
            data.createuid = new SessionObject().SessionUserID;
            data.createtime = DateTime.Now;
            data.you_code = you_code;

            if (this.hidd_no.Value == "")
            {
                dao.Add_youtube(data);
            }

            dao.Update();

            //更新父代網頁
            JsUtil.UpdateParentJs(this, str);
        }
    }


    private bool check_Input()
    {
        if (string.IsNullOrEmpty(this.tbox_name.Text))
        {
            JsUtil.AlertJs(this, "請輸入影片名稱!");
            return false;
        }

        if (string.IsNullOrEmpty(this.tbox_link.Text))
        {
            JsUtil.AlertJs(this, "請輸入影片網址!");
            return false;
        }

        int you_length = this.tbox_link.Text.Length;
        int you_star = this.tbox_link.Text.IndexOf("?v=");
        int you_end = this.tbox_link.Text.IndexOf("&");

        if (you_star == -1)
        {
            JsUtil.AlertJs(this, "YouTube 影片網址中含有「?v=」參數!");
            return false;
        }

        if (you_end == -1)
        { 
            you_end = you_length;                
        }

        you_code = this.tbox_link.Text.Substring(you_star+3, you_end - you_star-3);


        return true;
    }
}